/****************************************************************************
*  Title: simple_edge.sc
*  Author: Bryant Smith
*  Date: 05/01/2011
*  Description: currenlty does not perform matching, only inculdes 
*  image receiver and sender.  A stub behavior passes the left image
*  image matching based on corner detection to be added
*  No image processing code but shows system design concepts
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


// define image size, idea being from a hw camera this would be fixed
#define IMG_ROWS 480
#define IMG_CLMS 640

import "c_queue";


/*
* Receives imput image from c_queue
* for now just received two images and choses one to send out.
* inqueue is image double array, out is unsigned char array
*/
behavior matchReceive(i_receiver ProcImageLeft, i_receiver ProcImageRight, 
		out unsigned char image_buffer_l[IMG_ROWS][IMG_CLMS],
		out unsigned char image_buffer_r[IMG_ROWS][IMG_CLMS]) 
{
	// image buffers these are currently dependent on the size and format of the image used
	unsigned char row_buffer_1[IMG_CLMS]; // this file is 4x4 since there are 3 bytes/pixel -> 1536 bytes per row
	//double image_buffer_L[IMG_ROWS][IMG_CLMS];  // input image buffer
	//double image_buffer_R[IMG_ROWS][IMG_CLMS];  // input image buffer
	int row, a, j;

	void main(void) 
	{				

		//printf("Image Matcher - Reading Images from queue \n");
		
		//********************
		// Load image from c_queue into memory
		//********************
		
		// read image from c_queue and load into image buffer
		for(row = 0; row < IMG_ROWS; row++) {

			// Load row with a row of the image
			ProcImageLeft.receive(row_buffer_1, (IMG_CLMS));	
			
			// place row into image buffer
			for(a = 0; a < (IMG_CLMS); a++){
				image_buffer_l[row][a] =  row_buffer_1[a]; 	
			}	

			// Load row with a row of the image
			ProcImageRight.receive(row_buffer_1, (IMG_CLMS));	
			
			// place row into image buffer
			for(a = 0; a < (IMG_CLMS); a++){
				image_buffer_r[row][a] =  row_buffer_1[a]; 	
			}				
			
		}		
		printf("Image Matcher - Finished reading images into memory.  Starting Image matching . . . .\n");	
	}
	
};


/*
* Normalizes image of array double and sends out pixels over c_queue, assumes
* image pixels are magnitude only, no negative values
*/
behavior normalizeSnd(in unsigned char image_buffer[IMG_ROWS][IMG_CLMS], i_sender OutQueue) 
{
	int row;
		
	void main(void) {
	
		for(row = 0; row < IMG_ROWS; row++)
		{
			// send image out
			OutQueue.send(image_buffer[row], (IMG_CLMS));
		}

		printf("Image Matcher - Image has been written out\n");
	}

};


// takes in two input images and performs matching
behavior imageMatcher(in unsigned char image_buffer_l[IMG_ROWS][IMG_CLMS], 
		in unsigned char image_buffer_r[IMG_ROWS][IMG_CLMS], i_sender OutQueue)
{
	// image buffers these are currently dependent on the size and format of the image used
//	unsigned char img_buf[IMG_ROWS][IMG_CLMS];
//	unsigned char image_buffer_l[IMG_ROWS][IMG_CLMS];
//	unsigned char image_buffer_r[IMG_ROWS][IMG_CLMS];
	
//	matchReceive  imgRcvr(ProcImageLeft, ProcImageRight, image_buffer_l, image_buffer_r);
	normalizeSnd  imgSender(image_buffer_l, OutQueue);

	void main(void) 
	{				
			//imgRcvr.main();
			//waitfor 5;// perform matching
			imgSender.main();

	}
	
};
